package in.co.codedoc.tomcat;

import in.co.codedoc.ioc.IOCContainer;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class InitializerServlet
	extends HttpServlet
{
	@Override
	public void init(ServletConfig config)
		throws ServletException
	{
		super.init(config);
		
		String applicationName = config.getInitParameter("ApplicationName");

		if(applicationName == null || applicationName.equals(""))
		{
			applicationName = "application";
		}
		
		IOCContainer.SetApplicationName(applicationName);
		
		IOCContainer.ScanClasspath();
		IOCContainer.InitializeComponents();
		IOCContainer.StartComponents();
	}
	
	@Override
	public void destroy()
	{
		IOCContainer.ShutdownComponents();
		
		super.destroy();
	}
		
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
		throws ServletException, IOException
	{
		throw new ServletException("This servlet should not be invoked.");
	}
}
